package com.cskaoyan.service.impl;

import com.cskaoyan.mapper.PermissionMapper;
import com.cskaoyan.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.*;

/**
 * @author Fantasylkh
 */
@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    PermissionMapper permissionMapper;

    @Override
    public Set<String> selectPermissionsByRoleIds(Integer[] roleIds) {
        HashSet<String> permissionSet = new HashSet<>();
        if (!ObjectUtils.isEmpty(roleIds)) {
            for (Integer roleId : roleIds) {
                String[] permissions = permissionMapper.selectApiByRoleId(roleId);
                permissionSet.addAll(Arrays.asList(permissions));
            }
        }
        return permissionSet;

    }

    @Override
    public Set<String> selectPermissionsByApis(Set<String> apiList) {
        HashSet<String> permissionSet = new HashSet<>();
        if (!ObjectUtils.isEmpty(apiList)) {
            for (String api : apiList) {
                String permission = permissionMapper.selectPermissionByApi(api);
                permissionSet.add(permission);
            }
        }
        return permissionSet;
    }
}
